#ifndef HTTPSERVER_H
#define HTTPSERVER_H

#include "TcpServer.hpp"
#include "HttpRequest.hpp"
#include "HttpResponse.hpp"

class HttpServer {
public:
    HttpServer() {
        CallBackPtr callbackptr = std::make_shared<std::function<void(ConnectionPtr)>>(std::bind(&HttpServer::httpConnectionCall, this, std::placeholders::_1));
        tcpServer_.setConnectionCallBack(callbackptr);
        callbackptr = std::make_shared<std::function<void(ConnectionPtr)>>(std::bind(&HttpServer::httpMessageCall, this, std::placeholders::_1));
        tcpServer_.setMessageCallBack(callbackptr);
    }

    ~HttpServer()=default;
    void start(unsigned short port);

    inline void setConnectionCall(const std::function<void (ConnectionPtr)>& callback){
        auto callbackptr = std::make_shared<std::function<void(ConnectionPtr)>>(callback);
        userConnectionCall_ = callbackptr;
    }
    inline void setMessageCall(const std::function<void (ConnectionPtr)>& callback){
        auto callbackptr = std::make_shared<std::function<void(ConnectionPtr)>>(callback);
        userMessageCall_ = callbackptr;
    }

    inline void setWebConnCloseCall(const std::function<void (ConnectionPtr)>& callback){
        auto callbackptr = std::make_shared<std::function<void(ConnectionPtr)>>(callback);
        userWSCloseCall_ = callbackptr;
    }

    void setRoute(std::string route, const std::function<void (ConnectionPtr)>& callback);


private:
    TcpServer tcpServer_;
    void httpConnectionCall(ConnectionPtr conn);
    void httpMessageCall(ConnectionPtr conn);
    CallBackPtr userMessageCall_;
    CallBackPtr userConnectionCall_;
    CallBackPtr userWSCloseCall_;
    std::map<std::string, CallBackPtr> routeHandlers_;

};



#endif